<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;

/**
 * 平台列表管理
 * Class User
 * @package app\worker\controller
 */
class Platform extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'Platform';

    /**
     * 平台列表管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '平台管理';
        $this->_query($this->table)
        ->dateBetween('create_at')
        ->where(['is_deleted' => '0'])
        ->like('name')
        ->equal('status')
        ->page();
    }

    protected function _index_page_filter(&$data) {
        //计算作业人员
        
    }

    /**
     * 添加平台信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    public function _add_form_result(&$id){
        //各种操作
    }

    /**
     * 修改平台信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        if(request()->isPost()){
            if(!isset($data['id'])){
                //自动填写平台编码
                $number = Db::name($this->table)->where('is_deleted', 0)->order('number desc, id desc')->value('number');
                if($number){
                    $data['number'] = sprintf("%03d", $number + 1);
                }
            }
            
        }
    }

    /**
     * 更改平台信息状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $this->_save($this->table, ['status' => input('status', '0')]);
    }

    /**
     * 删除平台信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    /**
     * 计价规则
     * @auth true
     */
    public function price_rules_index()
    {
    }
}
